﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace QuickWPFMonthCalendar.DB
{
    public class Execute
    {

        public List<Error> _error = new List<Error>();

        protected Connection conn;


        public Execute()
        {
            conn = new Connection();
        }

        #region Diary
        public void Insert_tblBody(List<Tables.Diary> Listdiary)
        {
            Error err = new Error();
            _error.Clear();

            List<List<Parameter>> ContainerList = new List<List<Parameter>>();

            foreach (Tables.Diary diary in Listdiary)
            {
                List<Parameter> ListParameter = new List<Parameter>();

                ListParameter.Add(Common.CreateParameter("Body", diary.Body));
                ListParameter.Add(Common.CreateParameter("DateFormat", diary.DateFormat));
                ListParameter.Add(Common.CreateParameter("LastUpdate", DateTime.Now.ToString()));
                ContainerList.Add(ListParameter);
            }
            if (conn.IUDProcess(Query.InsertDiary, ContainerList))
            {
                err.Messege = conn.ErrorMessage;
                err.Proccess = "tblBody";
            }

        }

        public void Update_tblBody(List<Tables.Diary> Listdiary)
        {
            Error err = new Error();
            _error.Clear();

            List<List<Parameter>> ContainerList = new List<List<Parameter>>();

            foreach (Tables.Diary diary in Listdiary)
            {
                List<Parameter> ListParameter = new List<Parameter>();

                ListParameter.Add(Common.CreateParameter("Body", diary.Body));
                ListParameter.Add(Common.CreateParameter("LastUpdate", DateTime.Now.ToString()));
                ListParameter.Add(Common.CreateParameter("DateFormat", diary.DateFormat));
                ContainerList.Add(ListParameter);
            }
            if (conn.IUDProcess(Query.UpdateDiary, ContainerList))
            {
                err.Messege = conn.ErrorMessage;
                err.Proccess = "tblBody";
            }

        }

        public void Delete_tblDiary(string diary)
        {
            Error err = new Error();
            _error.Clear();
            List<List<Parameter>> ContainerList = new List<List<Parameter>>();

            List<Parameter> ListParameter = new List<Parameter>();

            ListParameter.Add(Common.CreateParameter("id", diary));

            ContainerList.Add(ListParameter);

            if (conn.IUDProcess(Query.DeleteDiary, ContainerList))
            {
                err.Messege = conn.ErrorMessage;
                err.Proccess = "Delete_DiarytoReference";
            }



        }

        public DataSet Select_tblBody()
        {
            DataSet ds = new DataSet();

            conn.Select(Query.SelectDiary, ref ds);

            return ds;
        }

        public DataSet Select_tblBody(DateTime dtime)
        {
            Error err = new Error();
            _error.Clear();

            DataSet ds = new DataSet();

            List<Parameter> ListParameter = new List<Parameter>();

            ListParameter.Add(Common.CreateParameter("LastUpdate", dtime.ToString()));


            if (conn.Select(Query.SelectDiaryOneDayForDate, ref ds, ListParameter))
            {
                err.Messege = conn.ErrorMessage;
                err.Proccess = "tblBody";
            }

            return ds;
        }


        public DataSet Select_tblBody(int Id)
        {
            Error err = new Error();
            _error.Clear();

            DataSet ds = new DataSet();

            List<Parameter> ListParameter = new List<Parameter>();

            ListParameter.Add(Common.CreateParameter("Id", Id.ToString()));


            if (conn.Select(Query.SelectDiaryOneDayForId, ref ds, ListParameter))
            {
                err.Messege = conn.ErrorMessage;
                err.Proccess = "tblBody";
            }

            return ds;
        }

        #endregion


        #region Images
        public void Insert_tblImages(List<Tables.Images> Listimages)
        {
            if (Listimages.Count <= 0)
                return;


            Error err = new Error();
            _error.Clear();
            List<List<Parameter>> ContainerList = new List<List<Parameter>>();

            foreach (Tables.Images images in Listimages)
            {
                List<Parameter> ListParameter = new List<Parameter>();
                ListParameter.Add(Common.CreateParameter("Id_diary", images.Id_diary));
                ListParameter.Add(Common.CreateParameter("Path", images.Path));


                ContainerList.Add(ListParameter);
            }


            Insert_tblImages(int.Parse(Listimages[0].Id_diary));

            if (conn.IUDProcess(Query.InsertImages, ContainerList))
            {
                err.Messege = conn.ErrorMessage;
                err.Proccess = "Insert_tblImages";
            }

        }

        public void Insert_tblImages(int Id)
        {
            Error err = new Error();
            _error.Clear();
            List<List<Parameter>> ContainerList = new List<List<Parameter>>();


            List<Parameter> ListParameter = new List<Parameter>();
            ListParameter.Add(Common.CreateParameter("Id_diary", Id.ToString()));



            ContainerList.Add(ListParameter);

            if (conn.IUDProcess(Query.DeleteImagesForId, ContainerList))
            {
                err.Messege = conn.ErrorMessage;
                err.Proccess = "Insert_tblImages";
            }

        }

        public DataSet Select_tblImages(int id_diary)
        {
            Error err = new Error();
            _error.Clear();

            DataSet ds = new DataSet();

            List<Parameter> ListParameter = new List<Parameter>();

            ListParameter.Add(Common.CreateParameter("id_diary", id_diary.ToString()));


            if (conn.Select(Query.SelectImagesForId, ref ds, ListParameter))
            {
                err.Messege = conn.ErrorMessage;
                err.Proccess = "Select_tblImages";
            }

            return ds;
        }

        public void Delete_tblImage(string id_diary)
        {
            Error err = new Error();
            _error.Clear();
            List<List<Parameter>> ContainerList = new List<List<Parameter>>();

            List<Parameter> ListParameter = new List<Parameter>();

            ListParameter.Add(Common.CreateParameter("id_diary", id_diary));

            ContainerList.Add(ListParameter);


            if (conn.IUDProcess(Query.DeleteImagesForId, ContainerList))
            {
                err.Messege = conn.ErrorMessage;
                err.Proccess = "Delete_tblImage";
            }

        }

        #endregion


        #region settings
        public DataSet Select_tblSettings()
        {
            Error err = new Error();
            _error.Clear();

            DataSet ds = new DataSet();

            List<Parameter> ListParameter = new List<Parameter>();


            if (conn.Select(Query.SelectSettings, ref ds, ListParameter))
            {
                err.Messege = conn.ErrorMessage;
                err.Proccess = "Select_tblSettings";
            }

            return ds;
        }


        public void Update_tblSettings(Tables.Settings Listsettings)
        {
            Error err = new Error();
            _error.Clear();

            List<List<Parameter>> ContainerList = new List<List<Parameter>>();


            List<Parameter> ListParameter = new List<Parameter>();

            ListParameter.Add(Common.CreateParameter("Value", Listsettings.color));
            ListParameter.Add(Common.CreateParameter("Id", "Color"));
           

            ContainerList.Add(ListParameter);

            if (conn.IUDProcess(Query.UpdateSettings, ContainerList))
            {
                err.Messege = conn.ErrorMessage;
                err.Proccess = "Update_tblSettings";
            }

        }

        #endregion
    }
}
